package jerry.html.data.convertors;

import java.util.List;
import java.util.ResourceBundle;

import jerry.html.data.Player;
import jerry.html.events.EvtCap;
import jerry.html.events.IsBotEvtCapImpl;
import jerry.html.events.PlayerEvtCapImpl;
import jerry.html.events.PlyrConnEvtCapImpl;
import jerry.html.events.PlyrDisconnEvtCapImpl;
import jerry.html.events.PlyrPingEvtCapImpl;
import jerry.html.events.PlyrRenameEvtCapImpl;
import jerry.html.events.PlyrSkillEvtCapImpl;
import jerry.html.events.PlyrTeamEvtCapImpl;
import jerry.html.events.PlyrTeamIdEvtCapImpl;
import jerry.html.events.PlyrTmNmEvtCapImpl;

/**
 * 
 * Used to convert player data
 * 
 * 
 * @author (h0t@_G0|i
 * 
 */
public class PlayerDataConvertor extends DataConvertor {

	@SuppressWarnings("unchecked")
	@Override
	public boolean convertData(List<? extends EvtCap> eventData) {
		boolean flag = false;
		if (dataType.equals(PlayerEvtCapImpl.class)) {
			try {
				for (PlayerEvtCapImpl evtCap : (List<PlayerEvtCapImpl>)eventData) {

					Player p = getPlayer((Integer)evtCap.subEvent.getClass().getField(
							"playerId").get(evtCap.subEvent));
					if (evtCap.subEvent instanceof PlyrConnEvtCapImpl) {
						PlyrConnEvtCapImpl evt = (PlyrConnEvtCapImpl) evtCap.subEvent;
						p.setPlayerName(evt.name);
						p.setPlayerPassword(evt.pwdHash);
						p.setTotalTime(evt.getTimeStamp() + p.getTotalTime());
					}
					if (evtCap.subEvent instanceof PlyrDisconnEvtCapImpl) {
						PlyrDisconnEvtCapImpl evt = (PlyrDisconnEvtCapImpl) evtCap.subEvent;
						p.setTotalTime(p.getTotalTime() - evt.getTimeStamp());
					}
					if (evtCap.subEvent instanceof PlyrPingEvtCapImpl) {
						PlyrPingEvtCapImpl evt = (PlyrPingEvtCapImpl) evtCap.subEvent;
						p.setPing(new Double(evt.ping));
					}
					if (evtCap.subEvent instanceof PlyrRenameEvtCapImpl) {
						PlyrRenameEvtCapImpl evt = (PlyrRenameEvtCapImpl) evtCap.subEvent;
						p.setPlayerName(evt.newName);
					}
					if (evtCap.subEvent instanceof PlyrSkillEvtCapImpl) {
						PlyrSkillEvtCapImpl evt = (PlyrSkillEvtCapImpl) evtCap.subEvent;
						p.setSkill(new Double(evt.skillValue));
					}
					if (evtCap.subEvent instanceof PlyrTeamEvtCapImpl) {
						PlyrTeamEvtCapImpl evt = (PlyrTeamEvtCapImpl) evtCap.subEvent;
						assignTeam(p, evt.teamId);
					}
					if (evtCap.subEvent instanceof PlyrTeamIdEvtCapImpl) {
						//PlyrTeamIdEvtCapImpl evt = (PlyrTeamIdEvtCapImpl) evtCap.subEvent;
						//assignTeam(p, evt.teamId);
					}
					if (evtCap.subEvent instanceof PlyrTmNmEvtCapImpl) {
						PlyrTmNmEvtCapImpl evt = (PlyrTmNmEvtCapImpl) evtCap.subEvent;
						p.setTeam(new Integer(ResourceBundle.getBundle("teams")
								.getString(evt.teamName)));
					}
					if (evtCap.subEvent instanceof IsBotEvtCapImpl) {
						IsBotEvtCapImpl evt = (IsBotEvtCapImpl) evtCap.subEvent;
						p.setBot(evt.isBot);
					}
				}
			} catch (Exception e) {
				System.err.println("Subclass "+ dataType + " doesn't have playerId field");
			}
			flag = true;
		}
		return flag;
	}

}
